﻿using MaterialDesignThemes.Wpf;
using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyFirstWPF.View.Compnent;

namespace MyFirstWPF.View
{
    /// <summary>
    /// UserControl2.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        TaskInfo MyTask;
        DiaryInfo MyDiary;
        public UserControl2(TaskInfo task)
        {
            InitializeComponent();
            showName.Text = task.Name;
            showDescription.Text = "  " + task.Description;
            showStartTime.Text = "  " + task.StartTime;
            showEndTime.Text = "  " + task.EndTime;
            if (task.isFinished == 1) {
                InitializeComponent();
                MyTask = task;
                TimeSpan time = DateTime.Now - task.GetEndTime();
                string pastDate = time.Days.ToString();
                string pastHour = time.Hours.ToString();                
                finishedTask.Text = "  " + task.Name + "\r\n" + "  " + task.EndTime + "    " + "距完成目标已经过去了：" +pastDate+"天"+ pastHour + "时" ;
                using (var db = new StudyPlanDb(Page1.dbPath))
                {
                    DiaryInfo diary = db.Diarys.Where(x => x.TaskId == task.Id).FirstOrDefault();
                    if (diary != null)
                    {
                        MyDiary = diary;
                    }
                }
            }
            else
            {
                InitializeComponent();
                MyTask = task;
                finishedTask.Foreground = Brushes.Red;
                finishedTask.Text = "  " + task.Name + "\r\n" + "  "+ "请尽快完成日志以结束任务哦";              
            }
        }

        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WrapPanel p = (WrapPanel)Parent;
            p.Children.Remove(this);
            using (var db = new StudyPlanDb(Page1.dbPath))
            {
                db.Tasks.Where(x => x.Id == MyTask.Id).Delete();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if(MyTask.isFinished == 1)
            {
                DialogHost.Show(new ShowDiary(MyDiary));
            } else
            {
                DialogHost.Show(new AddDiary(MyTask));
            }
        }
    }
}
